package com.weirong.smartvault.backservice.entity_model;

import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.DeliveryDetailLogRepository;
import com.weirong.smartvault.dao.DepartmentRepository;
import com.weirong.smartvault.dao.ForeignCurrencyRequestRepository;
import com.weirong.smartvault.entity.DeliveryDetailLog;
import com.weirong.smartvault.backservice.user.IUser;
import com.weirong.smartvault.entity.DeliveryLog;
import com.weirong.smartvault.entity.Department;
import com.weirong.smartvault.entity.ForeignCurrencyRequest;
import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.common.DateUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 于2017/11/29由 KFEB4 创建 .
 */
public class DeliveryLogModel implements ListModelInterface {
    private DeliveryLog deliveryLog;
    private ForeignCurrencyRequestRepository foreignCurrencyRequestRepository;
    private DepartmentRepository departmentRepository;
    private List<String> keys;
    private List<String> keyNames;
    DeliveryDetailLogRepository deliveryDetailLogRepository;

    public DeliveryLogModel(DeliveryLog deliveryLog){
        this.deliveryLog = deliveryLog;
        foreignCurrencyRequestRepository = SpringContextUtil.getBean(ForeignCurrencyRequestRepository.class);
        departmentRepository = SpringContextUtil.getBean(DepartmentRepository.class);
        deliveryDetailLogRepository = SpringContextUtil.getBean(DeliveryDetailLogRepository.class);
        keyNames = new ArrayList<>();
        keyNames.add("出库时间");
        keyNames.add("对应网点");
        keyNames.add("出库操作人");
        keys = new ArrayList<>();
        keys.add("getDeliveryTime");
        keys.add("getDepartmentName");
        keys.add("getHandlerName");

    }
    public String getDeliveryTime(){
        return DateUtil.getNomalString(deliveryLog.getDeliveryTime());
    }
    public String getDepartmentName(){
        Integer id = deliveryLog.getRequestId();
        ForeignCurrencyRequest foreignCurrencyRequest = foreignCurrencyRequestRepository.getOne(id);
        Department department= departmentRepository.findOne(foreignCurrencyRequest.getBranchId());
        return department.getName();
    }
    public String getHandlerName(){
        Integer id = deliveryLog.getHandler();
        IUser user = IUser.getOne(id);
        return user.getUserName();
    }
    public Integer getId(){
        return deliveryLog.getId();
    }

    @Override
    public List<String> keys() {
        return keys;
    }

    @Override
    public List<String> keyNames() {
        return keyNames;
    }


    public List<MoneyLogModel> getRequestDetail() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Integer id= this.deliveryLog.getRequestId();
        ForeignCurrencyRequest foreignCurrencyRequest = foreignCurrencyRequestRepository.findOne(id);
        ForeignCurrencyRequestModel foreignCurrencyRequestModel = new ForeignCurrencyRequestModel(foreignCurrencyRequest);
        return foreignCurrencyRequestModel.getAllDetailLogs();
    }

    public List<MoneyLogModel> getDeliveryDetail() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Integer id = getId();
        List<DeliveryDetailLog> detailLogs = deliveryDetailLogRepository.findAllByDeliveryId(id);
        return ListTool.cast(detailLogs,MoneyLogModel.class);
    }
}
